package pandabay2.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pandabay2.DAO.AuctionDAO;
import pandabay2.domain.Auction;

public class ExtSearchServlet extends HttpServlet{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		RequestDispatcher view = request.getRequestDispatcher("ExtSearch.jsp");
		view.forward(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			System.out.println("In the Extended Search Servlet");
			List<Auction> auctions;
			String tmpCategory = request.getParameter("category");
			String searchString = request.getParameter("searchString");
			int min = Integer.parseInt(request.getParameter("min"));
			int max = Integer.parseInt(request.getParameter("max"));
			if(tmpCategory.equals("Brettspiel"))
				auctions = AuctionDAO.extFindString(searchString, "Brettspiel", min, max);
			else if(tmpCategory.equals("Stofftier"))
				auctions = AuctionDAO.extFindString(searchString, "Stofftier", min, max);
			else if(tmpCategory.equals("Kinderbuch"))
				auctions = AuctionDAO.extFindString(searchString, "Kinderbuch", min, max);
			else {
				auctions = AuctionDAO.extFindString(searchString, "", min, max);
			}
			request.setAttribute("auctions", auctions);
			request.getRequestDispatcher("/searchResult.jsp").forward(request, response);
		} catch (Throwable exc) {
			System.out.println(exc);
		}
	}
}
